package business;

import java.util.ArrayList;

import bean.Order;
import database.DB_Handler;
import exceptions.IdNotFoundException;

/**
 * Business logic handler for all order functions which have to be passed to the database layer 
 * @author kaeufler.marcel
 */
public class BL_Order {
	
	/**
	 * @return returns an ArrayList with all orders from the database
	 */
	public ArrayList<Order> getAll(){
		ArrayList<Order> orders = DB_Handler.getInstance().getAllOrders();
			for(Order order:orders){
				try {
					order.setCustomer(new BL_Customer().getById(order.getCustomer_id()));
				} catch (IdNotFoundException e) {}
			}
		return orders;
	}
	
	/**
	 * @param id the order id of the order that should be selected from the database
	 * @return returns the order with the given id
	 * @throws IdNotFoundException if the given id is not found in the database
	 */
	public Order getById(int id) throws IdNotFoundException {
		DB_Handler handler = DB_Handler.getInstance();
		
		Order order = handler.getOrder(id);
		order.setCustomer(new BL_Customer().getById(order.getCustomer_id()));
		return order;
	}
	
	/**
	 * Adds an order to the database
	 * @param order order that should be added to the database
	 * @return Returns the same order but with it's new id which is created by the database
	 */
	public Order add(Order order){
		return DB_Handler.getInstance().addOrder(order);
	}
}
